#pragma once
#ifndef _CAMERA_H_
#define _CAMERA_H_

#pragma execution_character_set("utf-8")

#include <iostream>
#include <QLabel>
#include <QQueue>
#include "GalaxyIncludes.h"
#include <opencv2/opencv.hpp>
#include "timeUtils.h"
#include "configuration.h"

using namespace std;


/// <summary>
/// 调用大恒相机常用API，启动相机，相机设置，实时预览，关闭相机等
/// </summary>
class Camera : public QObject, public ICaptureEventHandler {
    
    Q_OBJECT

signals:

    //接收到图像的信号
    void frameReady(cv::Mat& frame);

    //上传图像
    void frameUpload(int num, cv::Mat& frame);

    ////开始采集的信号
    //void collectStart();

    ////停止采集的信号
    //void collectStop();

public:

    explicit Camera(QObject* parent = nullptr);

    ~Camera();

    /// <summary>
    /// 列出所有的相机， 在使用大恒的USB相机时，必须先列出所有的相机，否则相机打不开
    /// </summary>
    /// <param name="canmeraList"></param>
    static void listCamera(QStringList& canmeraList);

    /// <summary>
    /// 打开相机
    /// </summary>
    /// <param name="num">相机的SN编号/ip地址/MAC地址</param>
    /// <returns></returns>
    bool open(QString num = "");

    /// <summary>
    /// 查询相机是否打开
    /// </summary>
    /// <returns></returns>
    bool isOpened();

    /// <summary>
    /// 设置相机参数
    /// </summary>
    /// <param name="config"></param>
    void setParams(const CameraConfig& config);

    /// <summary>
    /// 设置图像裁取的区域
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    void setROI(int x, int y, int width, int height);

    //发送开采指令，相机开始曝光，在正式采集图形之前调用该函数
    void startLive();

    /// <summary>
    /// 发送停采指令，相机接收曝光，图像采集完成后调用该函数
    /// </summary>
    void stopLive();

    /// <summary>
    /// 开始采图
    /// </summary>
    void startCollect(int frameCount);

    /// <summary>
    /// 停止采集图像
    /// </summary>
    void stopCollect();

    /// <summary>
    /// 判断是否已经开启了图像采集
    /// </summary>
    /// <returns></returns>
    bool isCollectStart() const;

private:

    void DoOnImageCaptured(CImageDataPointer& objImageDataPointer, void* pUserParam);

public:

    QString imgdir;
    //控制是否开启采图模式
    atomic_bool m_collectFlag = false;
    // 记录本次采集的帧数
    int m_frameNum = 0;
    // 每次采集的帧数
    int m_frameCount = 490;
private:

    //相机的序列号/ip/MAC
    QString m_name;

    //指向相机的指针
    CGXDevicePointer m_objDevicePtr;

    //流控制对象
    CGXStreamPointer m_streamPtr;

    //最后一次采集图像的时间
    qint64 m_lastFrameTime = 0;

    

              

       

};
#endif //
